<template>
  <div>
    <el-dialog title="新增" :visible.sync="show" :before-close="closeDialog" :close-on-click-modal="true" width="600px" custom-class="dialog-dliveryDetail user-dialog-css" center>
      <el-form ref="form" v-loading="loading" :model="form" label-position="left" label-width="80px">
        <el-form-item label="用户名" prop="name" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item
          label="密码"
          prop="pwd"
          :rules="[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { pattern: /^\S{6,18}$/, message: '密码为6位到18位', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.pwd" show-password />
        </el-form-item>
        <el-form-item label="身份" prop="ident" :rules="[{ required: true, message: '请选择身份', trigger: 'blur' }]">
          <el-select v-model="form.ident" placeholder="请选择">
            <el-option v-for="item in USER_IDENT" :key="item.value" :label="`${item.value} (${item.type})`" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { userManagement } from '@/api/userManagement'
import { USER_IDENT } from '@/constants/user.constants'
export default {
  name: 'AddDialog',
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      loading: false,
      USER_IDENT
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.form = {
          name: '',
          pwd: '',
          ident: ''
        }
        //
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true
          await userManagement.userPut({ ...this.form, sessionId: this.$store.state.user.session_id })
          this.$message({
            message: '新增成功',
            type: 'success'
          })
          this.loading = false
          this.afterSubmit()
        } else {
          return false
        }
      })
    },
    afterSubmit() {
      this.$emit('afterSubmit')
    },
    closeDialog() {
      this.$emit('closeDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input.is-disabled .el-input__inner {
    color: #606266;
    background-color: #fff;
}
::v-deep .el-dialog--center .el-dialog__body {
    padding: 25px 60px 25px;
}
.info-text {
    font-size: 16px;
    font-weight: 500;
}
</style>
